package Interface;

import Engine.System1;

import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class MyInterface extends JFrame {
    JLabel topLabel;
    JLabel dayLabel;
    public JTextArea evenText;
    JTextArea foodArea;
    JTextArea item;
    public JPanel buttonPanel;

    JLabel background;

    /**
     * 背景图片
     */
    public static URL headerUrl = System1.class.getResource("/Interface/img.png");
    public static ImageIcon header = new ImageIcon(headerUrl);


    public MyInterface() {
        setSize(800, 800);
        setLayout(null);
        setVisible(true);

        topLabel = new JLabel();
        topLabel.setOpaque(true);
        topLabel.setBackground(new Color(0xf8ffff));
        topLabel.setBounds(0, 0, 650, 50);
        topLabel.setBorder(BorderFactory.createLineBorder(new Color(0xbed2c3), 2, true));
        getLayeredPane().add(topLabel, 0);

        dayLabel = new JLabel();
        dayLabel.setOpaque(true);
        dayLabel.setBackground(new Color(0xf8ffff));
        dayLabel.setBounds(650, 0, 150, 50);
        dayLabel.setBorder(BorderFactory.createLineBorder(new Color(0xbed2c3), 2, true));
        getLayeredPane().add(dayLabel, 0);

        background = new JLabel();
        background.setOpaque(true);
        background.setLayout(null);
        background.setBackground(Color.gray);
        background.setBounds(0, 50, 800, 500);
        getLayeredPane().add(background, 1);

        evenText = new JTextArea();
        evenText.setBounds(25, 75, 750, 275);
        evenText.setLineWrap(true);
        evenText.setBackground(new Color(255, 255, 255, 150));
        evenText.setFont(new Font("宋体", Font.BOLD, 18));
        evenText.setEditable(false);
        getLayeredPane().add(evenText, 0);

        buttonPanel = new JPanel();
        buttonPanel.setLayout(null);
        buttonPanel.setBounds(0, 350, 800, 200);
        buttonPanel.setBackground(new Color(0, 0, 0, 100));
        getLayeredPane().add(buttonPanel, 0);

        foodArea = new JTextArea();
        foodArea.setBackground(new Color(0xf8ffff));
        foodArea.setBounds(0, 550, 400, 250);
        foodArea.setBorder(BorderFactory.createLineBorder(new Color(0xbed2c3), 2, true));
        foodArea.setLineWrap(true);
        foodArea.setFont(new Font("宋体", Font.BOLD, 20));
        foodArea.setEditable(false);
        getLayeredPane().add(foodArea, 0);

        item = new JTextArea();
        item.setBackground(new Color(0xf8ffff));
        item.setBounds(400, 550, 250, 250);
        item.setLineWrap(true);
        item.setBorder(BorderFactory.createLineBorder(new Color(0xbed2c3), 2, true));
        item.setFont(new Font("宋体", Font.BOLD, 20));
        item.setEditable(false);
        getLayeredPane().add(item, 0);
        setBackground(header);
    }

    public void setTopLabel(String text) {
        topLabel.setText(text);
    }

    public void setBackground(ImageIcon icon) {
        icon.setImage(icon.getImage().getScaledInstance(background.getWidth(), background.getHeight(), Image.SCALE_DEFAULT));
        background.setIcon(icon);
    }

    public void setDayLabel(int day) {
        dayLabel.setText("第" + day + "天");
    }

    public void setEvenText(String message) {
        evenText.setText(message);
    }

    public void setFood(String food) {
        foodArea.setText(food);
    }

    public void setItem(String items) {
        item.setText(items);
    }

    public JButton[] showButton(String[] options) {
        try {
            buttonPanel.removeAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
        buttonPanel.repaint();
        buttonPanel.validate();
        buttonPanel.invalidate();
        buttonPanel.validate();
        int i = 0;
        int j = 0;
        JButton[] buttons = new JButton[options.length];
        for (String option : options) {
            JButton button = new JButton(option);
            button.setBounds(40 + i * 240, 40 + j * 90, 200, 50);
            buttonPanel.add(button);
            button.setBorderPainted(false);
            button.setBackground(new Color(0xf8ffff));
            button.requestFocus();
            buttons[3 * j + i] = button;
            if (i == 2) {
                i = 0;
                j++;
            } else {
                i++;
            }
        }
        return buttons;
    }
}
